package com.gitee.neuray.security.admin.org.controller;

import com.alibaba.fastjson.JSON;
import com.gitee.neuray.security.admin.annotation.FilterByDeletedAndOrderHandler;
import com.gitee.neuray.security.admin.constant.AdminCommonConstant;
import com.gitee.neuray.security.admin.org.entity.AdminOrgEntity;
import com.gitee.neuray.security.admin.org.biz.AdminOrgBiz;
import com.gitee.neuray.security.admin.user.biz.AdminStaffBiz;
import com.gitee.neuray.security.admin.user.entity.AdminStaffEntity;
import com.gitee.neuray.security.admin.vo.OrgTreeAll;
import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.util.TreeUtil;
import com.gitee.neuray.security.common.util.UUIDUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import java.util.*;


/**
 * 代码生成
 *
 * @author zhoyou
 * @email ${email}
 * @date 2020-03-10 17:12:43
 */
@RestController
@RequestMapping("pmOrg")
public class AdminOrgController extends BaseController<AdminOrgBiz, AdminOrgEntity>{
    @Autowired
    private AdminStaffBiz userBiz;

    /**
     * @param  org 参数列表
     * @return 结果集
     */
    @Override
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<AdminOrgEntity> add(@RequestBody AdminOrgEntity org) {
        //添加空字段默认值 以及pathcode  pathname  值
        String id = UUIDUtils.generateShortUuid();
        org.setId(id);
        org.setDeleted(AdminCommonConstant.ORG_DELETED_CODE);
        org.setPathCode(org.getPathCode()+id+AdminCommonConstant.ORG_PATH_CODE);
        org.setPathName(org.getPathName()+org.getOrgName()+AdminCommonConstant.ORG_PATH_NAME);
        org.setOrgLevel(org.getOrgLevel()+1);
        baseBiz.insertSelective(org);
        return new ObjectRestResponse<AdminOrgEntity>().rel(true);
    }

    /**
     * 更新方法
     * 同步更新所有子级的pathname  pathcode
     * @param org
     * @return
     */
    @Override
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<AdminOrgEntity> update(@RequestBody AdminOrgEntity org) {
        String parentPathName = org.getPathName();
        String parentPathCode = org.getPathCode();
        org.setPathName(parentPathName+org.getOrgName()+AdminCommonConstant.ORG_PATH_NAME);
        org.setPathCode(parentPathCode+org.getId()+AdminCommonConstant.ORG_PATH_CODE);
        AdminOrgEntity o = new AdminOrgEntity();
        o.setParentId(org.getId());
        List<AdminOrgEntity> orgs = baseBiz.selectList(o);
        baseBiz.updateSelectiveById(org);
        //根据id查找子节点
        if(orgs.size()>0){
            for(AdminOrgEntity or:orgs){
                or.setPathName(org.getPathName()+or.getOrgName()+AdminCommonConstant.ORG_PATH_NAME);
                or.setPathCode(org.getPathCode()+or.getId()+AdminCommonConstant.ORG_PATH_CODE);
                baseBiz.updateSelectiveById(or);
            }
        }
        return new ObjectRestResponse<AdminOrgEntity>().rel(true);
    }

    /**
     * 删除方法  如果组织下存在用户不能进行删除
     * @param id
     * @return
     */
    @Override
    public ObjectRestResponse<AdminOrgEntity> remove(@PathVariable String id) {
        AdminStaffEntity user = new AdminStaffEntity();
        user.setDeptCode(id);
        List<AdminStaffEntity> userList = userBiz.selectList(user);
        if(userList.size()>0){
            ObjectRestResponse<AdminOrgEntity> or = new ObjectRestResponse<>();
            or.setMessage("该组织下存在用户,暂时无法删除");
            or.setStatus(500);
            return or;
        }
        return super.remove(id);
    }
    /**
     * 获取组织树
     * @param map
     * @return
     */
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public ListRestResponse<List<OrgTreeAll>> tree(@RequestParam Map<String, String> map) {
        String parentTreeId = map.get("parentTreeId");
        if (StringUtils.isEmpty(parentTreeId)) {
            parentTreeId = AdminCommonConstant.ROOT;
        }
        List<AdminOrgEntity> orgs = ((AdminOrgController) AopContext.currentProxy()).getOrgs();
        List<OrgTreeAll> tree = getTree(orgs, parentTreeId);
        return new ListRestResponse("", tree.size(), tree);
    }
    @FilterByDeletedAndOrderHandler
    public List<AdminOrgEntity> getOrgs(){
        return baseBiz.selectListAll();
    }

    private List<OrgTreeAll> getTree(List<AdminOrgEntity> orgs, String parentTreeId) {
        List<OrgTreeAll> trees = new ArrayList<>();
        OrgTreeAll node;
        for (AdminOrgEntity org : orgs) {
            node = new OrgTreeAll();
            String jsonNode = JSON.toJSONString(org);
            node = JSON.parseObject(jsonNode, OrgTreeAll.class);
            node.setLabel(org.getOrgName());
            node.setOrder(org.getOrderId());
            node.setLevel(org.getOrgLevel());
            node.setPathCode(org.getPathCode());
            node.setPathName(org.getPathName());
            node.setDwlx(org.getDwlx());
            trees.add(node);
        }
        Collections.sort(trees, Comparator.comparing(OrgTreeAll::getOrder));
        return TreeUtil.bulid(trees, parentTreeId);
    }

}
